package nl.weeaboo.sh.level.l99;

import java.awt.event.KeyEvent;

import nl.weeaboo.game.gl.BlendMode;
import nl.weeaboo.game.gl.GLImage;
import nl.weeaboo.game.gl.GLManager;
import nl.weeaboo.game.input.UserInput;
import nl.weeaboo.sh.Game;
import nl.weeaboo.sh.danmaku.SpellUtil;
import nl.weeaboo.sh.danmaku.UniverseAdapter;
import nl.weeaboo.sh.level.Level;
import nl.weeaboo.sh.level.Room;
import nl.weeaboo.sh.level1.Boss01;
import nl.weeaboo.sh.level1.Chandelier;
import nl.weeaboo.sh.level1.FlyingSkull;
import nl.weeaboo.sh.level1.Skull;
import nl.weeaboo.sh.objects.Boss;
import nl.weeaboo.sh.objects.Platform;

public class Level99 extends Level {

	private GLImage nineballI;
	private GLImage bgI;
	private int boss01Timeout = -1;
	private int boss01DeadTimeout = -1;
	
	//private Point positions[];
	
	public Level99(Game game) {
		super(game, 99, 0);
	}

	//Functions
	public void loadResources() {				
		super.loadResources();	
		
		if (game.config.getSoundVolume() >= 0.001f) {
			if (game.getFileExists("snd/bgm.mp3")) {
				game.getSoundManager().setBGM("bgm.mp3", 5000);
			}
		}		
		
		nineballI = game.getImage("level99/circled-nine.png", false);
		bgI = game.getImage("level99/bg.png", true);
	}
		
	public void update(UserInput input, float dt) {
		super.update(input, dt);
		
		boolean bossCreated = false;
		
		if (input.consumeKey(KeyEvent.VK_NUMPAD1)) {
			Skull enemy = new Skull(game, this);
			enemy.setXY(camera.getX() + 300, camera.getY() - 300);
			addSprite(enemy);
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD2)) {
			FlyingSkull enemy = new FlyingSkull(game, this);
			enemy.setXY(camera.getX() + 300, camera.getY());
			addSprite(enemy);
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD3)) {
			Platform enemy = new Platform(game, this, game.getImage("platform.png", true));
			enemy.setXY(camera.getX(), camera.getY());
			addSprite(enemy);
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD4)) {
			Chandelier enemy = new Chandelier(game, this, game.getImage("platform.png", true));
			enemy.setXY(camera.getX(), camera.getY());
			addSprite(enemy);
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD5)) {
			for (int n = 0; n < 100; n++) {
				Skull enemy = new Skull(game, this);
				enemy.setXY(camera.getX() + 300, camera.getY() - 300);
				addSprite(enemy);
			}
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD6)) {
			Boss01 boss = new Boss01(game, this);
			boss.setXY(camera.getX() + 300, camera.getY());
			setBoss(boss);
			
			bossCreated = true;
		} else if (input.consumeKey(KeyEvent.VK_NUMPAD7)) {
			Cirno boss = new Cirno(game, this);
			boss.setXY(camera.getX() + 300, camera.getY());
			setBoss(boss);
			
			bossCreated = true;
		} else if (input.consumeKey(KeyEvent.VK_MULTIPLY)) {
			player.destroy();
		} else if (input.consumeKey(KeyEvent.VK_PAGE_UP)) {
			camera.setScale(camera.getScale() + 0.1f);
		} else if (input.consumeKey(KeyEvent.VK_PAGE_DOWN)) {
			camera.setScale(camera.getScale() - 0.1f);
		}
		
		if (bossCreated || input.consumeKey(KeyEvent.VK_DIVIDE)) {			
			if (getUniverseAdapter() == null) {
				Room room = getRoom(
						Level.toTileCoord(player.getX()),
						Level.toTileCoord(player.getY()));
				if (room != null) {
					setUniverseAdapter(new UniverseAdapter(game, room));
				}
			} else {
				setUniverseAdapter(null);
			}
		}
		
		if (boss01Timeout > 0) {
			boss01Timeout--;
			if (boss01Timeout == 0) {
				Room room = getRoom("boss01Room");
				if (room != null) {
					setUniverseAdapter(new UniverseAdapter(game, room));
	
					int ts = Level.tileSize;
					
					Cirno boss = new Cirno(game, this);
					boss.setXY((room.getX()+room.getWidth() - 15) * ts,
							(room.getY()+room.getHeight()/2) * ts);
					setBoss(boss);
				}
			}
		}
		
		if (boss01DeadTimeout > 0) {
			boss01DeadTimeout--;
			if (boss01DeadTimeout == 0) {
				//game.setLevel(new MainMenu(game));
			}
		}
	}
	
	public void onBossDefeated(Boss boss) {
		super.onBossDefeated(boss);
		
		setUniverseAdapter(null);
		Room room = getRoom("boss01Room");
		if (room != null) {
			room.setOpenSides(true, false, true, false);
			room.setWallSides(false, true, true, false);
		}
		
		boss01DeadTimeout = 240;
	}
	
	public void onPlayerChangeRoom(Room oldRoom, Room newRoom) {
		super.onPlayerChangeRoom(oldRoom, newRoom);
		
		if (newRoom == null) return;
		
		if (boss01Timeout == -1 && newRoom.getId().equals("boss01Room")) {
			boss01Timeout = 240;			
		}
	}
	
	public void drawBackground(GLManager glm, int w, int h) {
		bgI.draw(glm, 0, 0, w, h);
		
		if (boss01Timeout >= 0) {
			float a = (240 - boss01Timeout) * 60f / 240f;
			int x = Math.round(w - 900*SpellUtil.fastCos(a));
			int y = Math.round(h - 700*SpellUtil.fastSin(a));
			
			nineballI.draw(glm, x, y);
			
			if (boss01Timeout > 0) {
				GLImage nineballGlowI = game.getImage("level99/circled-nine-glow.png", false);

				glm.setBlendMode(BlendMode.LIGHT);
				glm.pushColor();
				glm.mixColor(1, 1, 1, boss01Timeout / 120f);
				nineballGlowI.draw(glm, x, y);
				glm.popColor();
				glm.setBlendMode(BlendMode.DEFAULT);
			}
		}
		
		/*
		Random random = new Random();		
		if (positions == null) {
			positions = new Point[32];
			for (int n = 0; n < positions.length; n++) {
				positions[n] = new Point(random.nextInt(w), random.nextInt(h));
			}
		}
		
		float angle = 360f * (float)((System.nanoTime() % 2000000000L) / 2000000000.0);		
		for (Point p : positions) {
			gl.glPushMatrix();
			gl.glTranslatef(p.x, p.y, 0);
			gl.glRotatef(angle + p.x * p.y, 0, 0, -1);
			nineballI.draw(glm, Math.round(-nineballI.getWidth()/2f),
					Math.round(-nineballI.getHeight()/2f));
			gl.glPopMatrix();
		}
		*/
	}
	
	public void drawForeground(GLManager glm, int w, int h) {
		if (boss01DeadTimeout == 0) {
			GLImage victoryI = game.getImage("level99/victory.png", false);
			victoryI.draw(glm, 0, 0, w, h);
		}
		
		super.drawForeground(glm, w, h);
	}

	//Getters
	
	//Setters
	
}
